package com.controller;

import com.pojo.Cart;
import com.service.CartService;
import com.vo.CartVo;
import com.vo.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author lifeng
 * @create 2022-11-28 11:17
 */
@RestController
@RequestMapping("/cart")
@CrossOrigin
@Slf4j
public class CartController {

    @Autowired
    private CartService cartService;

    /**
     * 根据用户id查询出该用户购物车中的所有商品
     */
    @PostMapping("/findByUid/{username}")
    public R finByUid(@PathVariable String username){
        //根据username查询出一条购物车记录，内部包含商品列表
        List<Cart> list = cartService.selectCartByUid(username);
        return R.ok().data("cartList",list);
    }

    /**
     * 添加到购物车
     */
    @PostMapping("/addCart")
    public R addCart(@RequestBody CartVo cartVo){
        log.info("获取:" + cartVo);
        cartService.addCart(cartVo);
        return R.ok().setMessage("成功加入"+ cartVo.getCount() +"个商品到购物袋");
    }

    @PostMapping("/updateCart")
    public R updateCart(@RequestBody List<Cart> carts){

        System.out.println("carts:" + carts);

        cartService.updateByCartIds(carts);
        return R.ok();
    }

}
